package com.example.studentapp.money;

import androidx.appcompat.app.AppCompatActivity;


import android.annotation.SuppressLint;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.media.audiofx.AudioEffect;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.studentapp.R;
import com.example.studentapp.beans.AIData;
import com.example.studentapp.beans.Money;
import com.example.studentapp.db.SQLiteDB;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.LimitLine;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.formatter.PercentFormatter;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;

import java.util.ArrayList;
import java.util.List;

public class AIRecordActivity extends AppCompatActivity {
    ImageView money_ai_returnIV;
    LineChart moneyLC;
    PieChart moneyPC;
    int[] colors = {Color.parseColor("#1C22F3"),Color.parseColor("#22EC43")};

    List<AIData> list;
    SQLiteDB sqLiteDB;
    SQLiteDatabase sqLiteDatabase;
    Description description; // 第三插件
    String[] titles;
    List<String> xDates;


    List<Money> pieList;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_airecord);
        init();

        getData();
        drawLines();
        getPieData();
        drawPie();
        /**
         *  主要是 画图第三方插件的 运用
         *  具体还不懂
         *
         * */

        money_ai_returnIV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    private void drawPie() {
        moneyPC.setUsePercentValues(true);
        moneyPC.setDrawEntryLabels(false);
        moneyPC.setDescription(description);

        moneyPC.setDrawHoleEnabled(true);
        moneyPC.setHoleColor(Color.WHITE);
        moneyPC.setHoleRadius(39f);
        moneyPC.setDrawCenterText(true);
        moneyPC.setCenterTextColor(Color.BLACK);
        moneyPC.setCenterTextSize(18f);
        moneyPC.setCenterText(getString(R.string.money_out_pie));

        ArrayList<PieEntry> pieEntries =new ArrayList<>();
        double pieCloth=0.0,pieFood=0.0,pieHouse=0.0,pieUse=0.0,pieWalk=0.0,pieOther=0.0;
        for(int i=0;i<pieList.size();i++){
            pieCloth+=pieList.get(i).getCloth();
            pieFood+=pieList.get(i).getFood();
            pieHouse+=pieList.get(i).getHouse();
            pieUse+=pieList.get(i).getUse();
            pieWalk+=pieList.get(i).getWalk();
            pieOther+=pieList.get(i).getOther();
        }

        pieEntries.add(new PieEntry((float)pieCloth,getString(R.string.money_cloth)));
        pieEntries.add(new PieEntry((float)pieFood,getString(R.string.money_food)));
        pieEntries.add(new PieEntry((float)pieHouse,getString(R.string.money_house)));
        pieEntries.add(new PieEntry((float)pieUse,getString(R.string.money_use)));
        pieEntries.add(new PieEntry((float)pieWalk,getString(R.string.money_walk)));
        pieEntries.add(new PieEntry((float)pieOther,getString(R.string.money_other)));

        PieDataSet pieDataSet=new PieDataSet(pieEntries,"");
        ArrayList<Integer> pColors=new ArrayList<>();
        pColors.add(Color.parseColor(getString(R.string.money_type_color1)));
        pColors.add(Color.parseColor(getString(R.string.money_type_color2)));
        pColors.add(Color.parseColor(getString(R.string.money_type_color3)));
        pColors.add(Color.parseColor(getString(R.string.money_type_color4)));
        pColors.add(Color.parseColor(getString(R.string.money_type_color5)));
        pColors.add(Color.parseColor(getString(R.string.money_type_color6)));
        pieDataSet.setColors(pColors);

        PieData pieData=new PieData(pieDataSet);
        pieData.setValueFormatter(new PercentFormatter());
        pieData.setValueTextSize(18f);
        pieData.setValueTextColor(Color.BLACK);
        moneyPC.setData(pieData);
        moneyPC.invalidate();
    }


    @SuppressLint("Range")
    private void getPieData() {
        String sql = "select * from money order by record_date desc limit 30";
        Cursor cursor = sqLiteDatabase.rawQuery(sql, null);
        if (cursor.getCount() > 0) {
            cursor.moveToFirst();
            for (int i = 0; i < cursor.getCount(); i++) {
                double cloth = cursor.getDouble(cursor.getColumnIndex("cloth"));
                double food = cursor.getDouble(cursor.getColumnIndex("food"));
                double house = cursor.getDouble(cursor.getColumnIndex("house"));
                double use = cursor.getDouble(cursor.getColumnIndex("use"));
                double walk = cursor.getDouble(cursor.getColumnIndex("walk"));
                double other = cursor.getDouble(cursor.getColumnIndex("other"));
                pieList.add(new Money(cloth, food, house, use, walk, other));
                cursor.moveToNext();
            }

        } else {
            Toast.makeText(getApplicationContext(),getString(R.string.money_sea_no),Toast.LENGTH_SHORT).show();
        }
    }

    private void drawLines() {
        ArrayList<ILineDataSet> dataSets = new ArrayList<>();
        for(int i =0;i<2;i++){
            ArrayList<Entry> values = new ArrayList<>();
            for(int j =0;j<list.size();j++){
                if(i==0){
                    values.add(new Entry(j,(float) list.get(j).getOut()));
                }else{
                    values.add(new Entry(j,(float) list.get(j).getIn()));
                }
            }
            LineDataSet dataSet = new LineDataSet(values,titles[i]);
            dataSet.setLineWidth(2.6f);
            dataSet.setColor(colors[i]);
            dataSet.setCircleRadius(3.2f);
            dataSet.setCircleColor(colors[i]);
            dataSet.setDrawCircleHole(false);
            dataSets.add(dataSet);
        }
        // 设置y轴样式
        YAxis yAxis_r = moneyLC.getAxisRight();
        yAxis_r.setEnabled(false);
        YAxis yAxis_l=moneyLC.getAxisLeft();
        yAxis_l.setEnabled(true);
        LimitLine limitLine=new LimitLine(150.0f,getString(R.string.money_warn_value));
        limitLine.setLineColor(Color.parseColor("#FF0101"));
        limitLine.setLineWidth(4.0f);
        yAxis_l.addLimitLine(limitLine);
        //设置x轴样式
        XAxis xAxis=moneyLC.getXAxis();
        xAxis.setAxisMinimum(0f);
        // xAxis.setAxisMaximum(xDates.size());
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                return xDates.get((int)value);
            }
        });
        LineData data = new LineData(dataSets);
        moneyLC.setData(data);
        moneyLC.setDescription(description);
        moneyLC.invalidate();
    }

    @SuppressLint("Range")
    private void getData() {
        list = new ArrayList<>();
        xDates = new ArrayList<>();
        String sql = "select * from money order by record_date desc limit 7";
        Cursor cursor = sqLiteDatabase.rawQuery(sql,null);
        if(cursor.getCount()>0){
            cursor.moveToFirst();
            for (int i = 0; i < cursor.getCount(); i++) {
                double cloth = cursor.getDouble(cursor.getColumnIndex("cloth"));
                double food = cursor.getDouble(cursor.getColumnIndex("food"));
                double house = cursor.getDouble(cursor.getColumnIndex("house"));
                double use = cursor.getDouble(cursor.getColumnIndex("use"));
                double walk = cursor.getDouble(cursor.getColumnIndex("walk"));
                double other = cursor.getDouble(cursor.getColumnIndex("other"));
                double get = cursor.getDouble(cursor.getColumnIndex("get_money"));
                double out =cloth+food+house+use+walk+other;
                String record_date=cursor.getString(cursor.getColumnIndex("record_date"));
                xDates.add(record_date.substring(5));
                list.add(new AIData(out,get));
                cursor.moveToNext();
            }
        }else{
            Toast.makeText(getApplicationContext(), getString(R.string.money_sea_no), Toast.LENGTH_SHORT).show();
        }
    }

    private void init() {
        money_ai_returnIV = findViewById(R.id.money_ai_returnIV);
        moneyLC= findViewById(R.id.moneyLC);
        moneyPC= findViewById(R.id.moneyPC);
        titles = new String[]{getString(R.string.money_out_o),getString(R.string.money_in_o)};

        description = new Description();
        description.setText("");

        pieList = new ArrayList<>();
        // 数据的获取
        sqLiteDB = new SQLiteDB(getApplicationContext());
        sqLiteDatabase = sqLiteDB.getWritableDatabase();

    }
}